package cn.zt.controller;

import cn.zt.dto.ResultDTO;
import cn.zt.model.FileModel;
import cn.zt.model.PictureModel;
import cn.zt.model.RotaionPicture;
import cn.zt.service.FileModelService;
import cn.zt.service.PictureModelService;
import cn.zt.util.GuidUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author: zt
 * @Date: 2020/6/9 17:42
 */
@RestController
@RequestMapping("/pictureVideo")
@Configuration
@Api(value = "pictureVideo", description = "图片视频接口")
public class PictureController {

    @Autowired
    private  PictureModelService pictureModelService;

    @Value(value = "${application.profile}")
    private String profile;

    @PostMapping(value = "/pictureUpload")
    @ApiOperation("保存图片是屁")
    public String pictureUpload(@RequestParam(value = "file") MultipartFile file, PictureModel pm, Model model, HttpServletRequest request) {
        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String filePath = profile; // 上传后的路径
        fileName = UUID.randomUUID() + suffixName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filename = "D:/temp-file/" + fileName;
        model.addAttribute("filename", filename);
        //保存图片路径和文件名到数据库
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            PictureModel pictureModel = new PictureModel();
            pictureModel.setId(GuidUtil.getGuid());
            pictureModel.setImgSrc(fileName);
            pictureModel.setName(pm.getName());
            pictureModel.setImgSrc(filename);
            pictureModel.setPrice(pm.getPrice());
            pictureModel.setPlayCount(0);
            pictureModel.setPaymentCount(0);
            pictureModel.setType(pm.getType());
            pictureModel.setTime(sdf.format(new Date()));
            pictureModelService.savePicture(pictureModel);


        } catch (Exception e) {
            e.printStackTrace();
        }
        return "保存成功";
    }


    @GetMapping(value = "/pictureVideoList")
    @ApiOperation("获取图片视频列表")
    public ResultDTO pictureVideoList() {

        List<PictureModel> pictureList = new ArrayList<>();
        try {
            pictureList = pictureModelService.queryPictureVideoList();


        } catch (Exception e) {
            return ResultDTO.isFail(e);
        }
        return ResultDTO.isOk().data(pictureList);
    }

}
